package com.lihepeng.leecode.array2.slid_window;

import java.util.HashSet;
import java.util.Set;

/**
 * 滑动窗口
 * 找出最大的
 */
public class Solution03 {
    public int lengthOfLongestSubstring(String s) {
        Set<Character> characters = new HashSet<>();
        int n = s.length();
        // 右侧指针
        int rk =-1;
        int ans = 0;
        for (int i=0;i<n;i++) {
            if (i!=0) {
                // 将左边的元素移动到窗口中
                characters.remove(s.charAt(i-1));
            }
            while (rk+1<n && !characters.contains(s.charAt(rk+1))) {
                characters.add(s.charAt(rk+1));
                ++rk;
            }
            ans = Math.max(ans,characters.size());
        }
        return ans;
    }
}
